package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.BlockList;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ActionFailed;
import labox.innovation.gameserver.network.serverpackets.SendTradeRequest;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 *  交易请求
 *  客户端协议
 * @author zjj
 *
 */
public final class TradeRequest extends L2GameClientPacket {
	private static final String TRADEREQUEST__C__15 = "[C] 0x73 TradeRequest";

	private static final Logger _log = LoggerFactory.getLogger(TradeRequest.class.getName());

	private long _objectId; //请求交易的对象的 objID

	private static final int _radius = 150;

	@Override
	protected void readImpl() {
		_objectId = readLong();
	}

	@Override
	protected void runImpl() {
		FuzePcInstance player = getClient().getActiveChar();
		if (player == null)
			return;

		if (!player.getAccessLevel().allowTransaction()) {
			player.sendMessage("Transactions are disable for your Access Level");
			sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}

		// 获得指定对象id的 角色
		// 交易的对象 要在玩家的可视链表中
		L2Object target = player.getKnownList().getKnownCharacters().get(_objectId);

		if (target == null || !(target instanceof FuzePcInstance) || (target.getObjectId() == player.getObjectId())) {
			if( _log.isDebugEnabled() )
				_log.debug("人物OBjectId 错误 :" + _objectId + player.getName(), Config.SERVER_ID);
			player.sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
			return;
		}

		//交易对象
		FuzePcInstance partner = (FuzePcInstance) target;

		// cant trade with players from other instance except from multiverse
		if (partner.getInstanceId() != player.getInstanceId() && player.getInstanceId() != -1)
			return;

		// Alt game - Karma punishment
		if (!Config.ALT_GAME_KARMA_PLAYER_CAN_TRADE && (player.getKarma() > 0 || partner.getKarma() > 0)) {
			player.sendMessage("Chaotic players can't use Trade.");
			return;
		}

		//正在和别人交易
		if (player.isProcessingTransaction()) {
			if (_log.isDebugEnabled())
				_log.debug("already trading with someone");
			player.sendPacket(new SystemMessage(SystemMessageId.ALREADY_TRADING));
			return;
		}

		// 目标在交易中
		if (partner.isProcessingRequest() || partner.isProcessingTransaction()) {
			if (Config.DEBUG)
				_log.debug("transaction already in progress.");
			SystemMessage sm = new SystemMessage(SystemMessageId.C1_IS_BUSY_TRY_LATER);
			sm.addString(partner.getName());
			player.sendPacket(sm);
			return;
		}

		//目标是否在 交易拒绝模式
		if (partner.getTradeRefusal()) {
			player.sendMessage("目标不允许交易");
			return;
		}

		//是否在黑名单中
		if (BlockList.isBlocked(partner, player)) {
			SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
			sm.addCharName(partner);
			player.sendPacket(sm);
			return;
		}

		if (!player.isInsideRadius(partner, _radius)) {
			SystemMessage sm = new SystemMessage(SystemMessageId.TARGET_TOO_FAR);
			player.sendPacket(sm);
			return;
		}

		player.onTransactionRequest(partner);
		//发送 交易消息给 交易的方
		partner.sendPacket(new SendTradeRequest(player.getObjectId(), player.getName()));

		SystemMessage sm = new SystemMessage(SystemMessageId.REQUEST_C1_FOR_TRADE);
		sm.addString(partner.getName());
		player.sendPacket(sm);

	}

	@Override
	public String getType() {
		return TRADEREQUEST__C__15;
	}
}
